﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PMSA.Framework.Log;

public partial class Admin_Uc_Product_EditProduct : System.Web.UI.UserControl
{
    private int productid = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["pi"] != null)
        {
            try
            {
                productid = int.Parse(Request.QueryString["pi"].ToString());
            }
            catch
            {
                CLogManager.Write("Product.Edit.Page_Load", "productid not a number: " + Request.QueryString["pi"].ToString(), "ADMIN");
            }
        }
        if (IsPostBack)
        {

        }
        else
        {
            CreateDDLGroupProduct();
            CreateDDLBrand();
            GetDetailProduct(productid.ToString());
        }
    }
    private void GetDetailProduct(string productId)
    {
        try
        {
            Product obj = new Product();
            obj = obj.Detail(productid);

            proName.Text = obj.ProductName;
            proUnit.Text = obj.ProductUnit;
            proPrice.Text = obj.ProductPrice.ToString();
            proDes.Text = obj.ProductDescription;
            proActive.Checked = obj.IsActive == 1 ? true : false;
            chooseGroupProduct.SelectedValue = obj.GroupProductId.ToString();
            chooseBrand.SelectedValue = obj.BrandId.ToString();
            //photos1.Value = obj.ProductImage;
            //photos2.Value = obj.Image1;
            //photos3.Value = obj.Image2;
            //photos4.Value = obj.Image3;
            //photos5.Value = obj.Image4;
        }
        catch (Exception ex)
        {
            CLogManager.Write("Product.Edit.GetDetailProduct", ex.ToString(), "ADMIN");
        }
    }
    private void CreateDDLGroupProduct()
    {
        try
        {
            GroupProduct obj = new GroupProduct();
            IList<GroupProduct> list = obj.List(1, 10000, 0, 0, -1);
            chooseGroupProduct.DataValueField = "GroupProductId";
            chooseGroupProduct.DataTextField = "GroupProductName";
            chooseGroupProduct.DataSource = list;
            chooseGroupProduct.DataBind();
        }
        catch (Exception ex)
        {
            CLogManager.Write("Product.Edit.CreateDDLGroupProduct", ex.ToString(), "ERR");
        }
    }
    private void CreateDDLBrand()
    {
        try
        {
            Brand obj = new Brand();
            IList<Brand> list = obj.List(1, 10000, 0);
            chooseBrand.DataValueField = "BrandId";
            chooseBrand.DataTextField = "BrandName";
            chooseBrand.DataSource = list;
            chooseBrand.DataBind();
        }
        catch (Exception ex)
        {
            CLogManager.Write("Product.Edit.CreateDDLBrand", ex.ToString(), "ERR");
        }
    }
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        try
        {
            string name = proName.Text;
            string unit = proUnit.Text;
            string price = proPrice.Text;
            string des = proDes.Text;
            int active = proActive.Checked ? 1 : 0;
            int groupid = int.Parse(chooseGroupProduct.SelectedValue);
            int brandid = int.Parse(chooseBrand.SelectedValue);
            string _photoNull = "../img/layout/blank.gif";
            string _photoLink = "../Admin/Upload/Image/Product/";
            #region photos

            string _photos1 = _photoNull;
            if (!String.IsNullOrEmpty(photos1.Value))
            {
                _photos1 = _photoLink + photos1.Value;
            }
            string _photos2 = _photoNull;
            if (!String.IsNullOrEmpty(photos2.Value))
            {
                _photos2 = _photoLink + photos2.Value;
            }
            string _photos3 = _photoNull;
            if (!String.IsNullOrEmpty(photos3.Value))
            {
                _photos3 = _photoLink + photos3.Value;
            }
            string _photos4 = _photoNull;
            if (!String.IsNullOrEmpty(photos4.Value))
            {
                _photos4 = _photoLink + photos4.Value;
            }
            string _photos5 = _photoNull;
            if (!String.IsNullOrEmpty(photos5.Value))
            {
                _photos5 = _photoLink + photos5.Value;
            }
            #endregion
            if ((new Product().Update(productid, name, unit, int.Parse(price), des, active, groupid, brandid, _photos1, _photos2, _photos3, _photos4, _photos5)) != null)
            {
                Response.Write("<script type='text/javascript'>alert('Edit Successful !');</script>");
            }
        }
        catch (Exception ex)
        {
            CLogManager.Write("Product.Edit.btnEdit_Click", ex.ToString(), "ADMIN");
        }
    }
}